package com.zhuiYun.project.api.handworkdiycollection.mapper;

import com.zhuiYun.project.api.handworkdiycollection.entity.dto.HandworkDiyCollectionDTO;
import com.zhuiYun.project.api.handworkdiycollection.entity.HandworkDiyCollection;
import com.zhuiYun.project.api.handworkdiycollection.model.HandworkDiyCollectionModel;
import com.zhuiYun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author yangze
 * @Description todo 手工收藏mapper
 * @Date 2023/4/6 10:27
 */

@Mapper
public interface HandworkDiyCollectionMapper extends BaseMapper<HandworkDiyCollection> {

    /**
     * @param userId
     * @param diyCommodityName
     * @Author yangze
     * @Description TODO 手工收藏列表查询
     * @Date 2023/4/6 10:37
     * @Return
     */
    @Select("SELECT\n" +
            " d.diy_name,d.diy_price,d.diy_favorable_price,d.diy_image_id,c.id\n" +
            "FROM\n" +
            " handwork_diy_collection c\n" +
            " LEFT JOIN handwork_diy d ON d.id = c.handwork_diy_id \n" +
            "WHERE\n" +
            " c.user_id = #{userId} \n" +
            " AND d.diy_name LIKE '%${diyCommodityName}%' \n" +
            " AND c.del_flag = 0 \n" +
            "ORDER BY\n" +
            " c.update_time DESC")
    List<HandworkDiyCollectionDTO> selectHandworkDiyCollectionList(@Param("userId") Integer userId,@Param("diyCommodityName") String diyCommodityName);

    /**
     * @param id
     * @Author yangze
     * @Description TODO 手工取消收藏
     * @Date 2023/4/10 9:41
     * @Return
     */
    @Update("UPDATE handwork_diy_collection SET del_flag = 1 WHERE id = #{id}")
    int deleteById(Integer id);


    /**
    * @Author yangze
    * @Description 根据用户ID和手工ID查询是否收藏
    * @Date 2023/4/10 14:40
    * @Return
    * @param userId
    * @param handworkDiyId
    */

    @Select("SELECT * FROM handwork_diy_collection WHERE user_id = #{userId} AND handwork_diy_id = #{handworkDiyId}")
    HandworkDiyCollection selectByUserIdAndHandworkDiyId(Integer userId, Integer handworkDiyId);

    /**
     * @param userId,handworkDiyId
     * @Author yz
     * @Description 修改商品收藏状态
     * @Date 2023/4/10 11:29
     * @Return int
     */
    @Update("UPDATE handwork_diy_collection \n" +
            "SET del_flag = 0, update_time = NOW()\n" +
            "WHERE user_id = #{userId} AND handwork_diy_id = #{handworkDiyId}")
    int updateByUserIdAndHandworkDiyId(Integer userId, Integer handworkDiyId);

    /**
    * @Author yangze
    * @Description 根据用户id和diy商品id取消收藏
    * @Date 2023/4/10 14:56
    * @Return
     * @param userId,handworkDiyId
    */
    @Update("UPDATE handwork_diy_collection \n" +
            "SET del_flag = 1, update_time = NOW()\n" +
            "WHERE user_id = #{userId} AND handwork_diy_id = #{handworkDiyId}")
    int deleteByUserIdAndDiyId(Integer userId, Integer handworkDiyId);
    /**
     * @Author 赵笛
     * @Description TODO 根据当前用户id查询收藏diy个数
     * @Date 2023/4/11 8:53
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param userId
     */
    @Select("select count(id) from handwork_diy_collection where user_id = #{userId} and del_flag = 0")
    Integer getDiyCollectionCountByUserId(Integer userId);
}
